package com.xhwl.centiotdata.suppliersystem.fushi;

import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.common.exception.ApplicationException;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.UUID;

/**
 * @Author: yanyixiong
 * @Description:
 * @Date: create in 14:13 2023/4/21
 */
@Slf4j
@Service
public class GeneralFuShiHandler {

    private JSONObject post(String path, Map<String, Object> params) {
        log.info("【FuShi post】path:{};params:{}", path, JSON.toJSONString(params));
        String result = null;
        try {
            result = HttpRequest
                    .post(path)
                    .body(JSON.toJSONString(params))
                    .execute()
                    .body();
        } catch (HttpException e) {
            log.error("【FuShi post】path:{};params:{}; request failed!", path, JSON.toJSONString(params));
            e.printStackTrace();
            throw new ApplicationException();
        }
        if (StringUtils.isEmpty(result)) {
            log.error("【FuShi post】path:{};params:{};result is null!", path, JSON.toJSONString(params));
            throw new BusinessException();
        }
        log.info("【FuShi post】path:{};params:{};result:{}", path, JSON.toJSONString(params), result);
        return JSON.parseObject(result);
    }

    private JSONObject put(String path, Map<String, Object> params) {
        log.info("【FuShi put】path:{};params:{}", path, JSON.toJSONString(params));
        String result = null;
        try {
            result = HttpRequest
                    .put(path)
                    .body(JSON.toJSONString(params))
                    .execute()
                    .body();
        } catch (HttpException e) {
            log.error("【FuShi put】path:{};params:{}; request failed!", path, JSON.toJSONString(params));
            e.printStackTrace();
            throw new ApplicationException();
        }
        if (StringUtils.isEmpty(result)) {
            log.error("【FuShi put】path:{};params:{};result is null!", path, JSON.toJSONString(params));
            throw new BusinessException();
        }
        log.info("【FuShi put】path:{};params:{};result:{}", path, JSON.toJSONString(params), result);
        return JSON.parseObject(result);
    }

    private JSONObject get(String path, Map<String, String> params) {
        log.info("【FuShi get】path:{};params:{}", path, JSON.toJSONString(params));
        String result = null;
        try {
            result = HttpRequest
                    .get(path)
                    .formStr(params)
                    .execute()
                    .body();
        } catch (HttpException e) {
            log.error("【FuShi get】path:{};params:{}; request failed!", path, JSON.toJSONString(params));
            e.printStackTrace();
            throw new ApplicationException();
        }
        if (StringUtils.isEmpty(result)) {
            log.error("【FuShi get】path:{};params:{};result is null!", path, JSON.toJSONString(params));
            throw new BusinessException();
        }
        log.info("【FuShi get】path:{};params:{};result:{}", path, JSON.toJSONString(params), result);
        return JSON.parseObject(result);
    }

    public JSONObject post(String api, Map<String, Object> params, String ipAndPort, String gid) {
        params.put("Gid",gid);
        StringBuffer pathBuffer = new StringBuffer()
                .append(ipAndPort)
                .append(api);
        return this.post(pathBuffer.toString(), params);
    }

    public JSONObject put(String api, Map<String, Object> params, String ipAndPort, String gid) {
        params.put("Gid",gid);
        StringBuffer pathBuffer = new StringBuffer()
                .append(ipAndPort)
                .append(api);
        return this.put(pathBuffer.toString(), params);
    }

    public JSONObject get(String api, Map<String, String> params, String ipAndPort, String gid) {
        StringBuffer pathBuffer = new StringBuffer()
                .append(ipAndPort)
                .append(api);
        return this.get(pathBuffer.toString(), params);
    }

    public String generaRid(){
        return UUID.randomUUID().toString().toUpperCase();
    }

    public String generaStaffNo(Integer projectId, Integer accountId){
        return "P"+projectId +"U"+accountId;
    }

    
}
